/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import Jama.Matrix;
import com.showyourwork.engine.Equation;
import com.showyourwork.engine.EquationBuilder;
import com.showyourwork.engine.EquationParser;
import edu.jas.arith.BigRational;
import edu.jas.integrate.ElementaryIntegration;
import edu.jas.integrate.QuotIntegral;
import edu.jas.kern.ComputerThreads;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.poly.TermOrder;
import edu.jas.ufd.Quotient;
import edu.jas.ufd.QuotientRing;

public class Solver {
    
    private String ecuacion = null;
    private static String[] vars = new String[]{"x"};
    private QuotientRing<BigRational> qfac;
    private GenPolynomial<BigRational> a;
    private GenPolynomial<BigRational> d;
    private GenPolynomialRing<BigRational> fac;
    private static BigRational br = new BigRational(0);
    private static EquationBuilder b = new EquationBuilder();
    private ElementaryIntegration<BigRational> eIntegrator;
    private Quotient<BigRational> q;
    private Quotient<BigRational> derivada;
    private QuotIntegral<BigRational> integral;
    private Equation e;
    private Matrix lhs;
    private Matrix rhs;
    private Matrix ans;
    private double [][]matrix;
    private double  []resultado;
    
    public Solver(String ecuacion){
        this.ecuacion = ecuacion;
    }
    
    public Solver(double [][]matrix){
        this.matrix = matrix;
    }
    
    public Solver(double [][]matrix,double []resultado){
        this.matrix = matrix;
        this.resultado = resultado;
    }
    
    public String solveEquation(){
        new EquationParser(b).parse(ecuacion);
        e = b.build();
        e.evaluateUsingNewtonsMethod();
        return e.getAnswers().toString();
    }
    
    public String integrate(){ 
        cargar();
        integral = eIntegrator.integrate(q);
        return integral.toString();
    }
    
    public String deriviative(){
        cargar();
        derivada = eIntegrator.deriviative(q);
        return derivada.toString();
    }
    
    public void cerrar(){
        ComputerThreads.terminate();
    }
    
    public double [][]matrix(){
        lhs = new Matrix(matrix);
        return lhs.inverse().getArray();
    }
    
    public String solveSystem(){
        lhs = new Matrix(matrix);
        rhs = new Matrix(resultado, resultado.length);
        ans = lhs.solve(rhs);
        String salida = "";
        for(int i = 0;i<resultado.length;i++){
            salida += ans.get(i, 0);
        }
        return salida;
    }
    
    public void cargar(){
        fac = new GenPolynomialRing<BigRational>(br, vars.length, new TermOrder(
                TermOrder.INVLEX), vars);
        qfac = new QuotientRing<BigRational>(fac);
        eIntegrator = new ElementaryIntegration<BigRational>(br);
        a = fac.parse(ecuacion);
        d = fac.parse("");
        q = new Quotient<BigRational>(qfac, a, d);
        
    }
}